package com.colorfruit.admin.control.config;


import com.colorfruit.admin.pojo.enums.ErrorCodeEnum;
import com.colorfruit.framework.model.pojo.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public BaseResult handlerException(RuntimeException e, HttpServletRequest request) {
        log.error("# [OUT] errorMessage: " + e.getMessage(), e);
        return new BaseResult(ErrorCodeEnum.RUNTIME_EXCEPTION.getCode(), ErrorCodeEnum.RUNTIME_EXCEPTION.getMsg());
    }

    @ExceptionHandler(Exception.class)
    public BaseResult handlerException(Exception e) {
        log.error("# [OUT] errorMessage: " + e.getMessage(), e);
        return new BaseResult(ErrorCodeEnum.UNKNOW_EXCEPTION.getCode(), ErrorCodeEnum.UNKNOW_EXCEPTION.getMsg());
    }
}
